<%@page import="java.io.InputStream"%>
<%@page import="cn.school.service.Service2"%>
<%@page import="cn.school.entity.Grade"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.2.0.js"></script>
<script>
   function PreviewImage(img) 
   { 
    var pattern = /(\.*.jpg$)|(\.*.png$)|(\.*.jpeg$)|(\.*.gif$)|(\.*.bmp$)/;      
    if(!pattern.test(img.value)) 
    { 
     alert("系统仅支持jpg/jpeg/png/gif/bmp格式的照片！");  
     img.focus(); 
    } 
    else 
    { 
     var path; 
     if(document.all)//IE 
     { 
      img.select(); 
      path = document.selection.createRange().text; 
      document.getElementById("imgPreview").innerHTML=""; 
      document.getElementById("imgPreview").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")";//使用滤镜效果 
     } 
     else//FF 
     { 
      path = URL.createObjectURL(img.files[0]);
      document.getElementById("imgPreview").innerHTML = "<img src='"+path+"'/>"; 
     } 
    } 
   } 
   
  /*  function createAjax(){
      if(window.XMLHttpRequest)
   	   return new XMLHttpRequest();
      
      return new ActiveXObject("Microsoft.XMLHTTP");  
   }
   function checkTele(t){
   	  if(t.value=="")
   		  {
   		  	return false;
   		  }
   	  //发送ajax请求验证
   	  var xhr=createAjax();
   	  xhr.onreadystatechange=function(){
   		  if(xhr.readyState==4&&xhr.status==200){
   			  var str=xhr.responseText;
   			  if(str=="true"){
   				  alert("此电话已经被注册了");
   			  }else{
   				  alert("此电话可以使用");
   			  }
   		  }
   	  }
   	  xhr.open("GET","checkTeleServlet?tele="+t.value,true);
   	  xhr.send(null);
     } */

     $(function(){
    	 $("#telephone").blur(function(){
    		 if(this.value==""){
    			 return false;
    		 }
    		 $.get("checkTeleServlet",{tele:$(this).val()},function(data){
    			 if(data=="true"){
      				  alert("此电话已经被注册了");
      			  }else{
      				  alert("此电话可以使用");
      			  }
    		 },"text");
    	 });
    	 
    	 $.get("GradeAjaxServlet",function(data){
    		 var g=$("#gradeid");
    		 $.each(data,function(i,d){
    			 g.append(new Option(d.gname,d.id));
    			 //g.append("<option value=''></option>");
    		 });
    	 });
     });
     
     var xhr = new  XMLHttpRequest();
	 xhr.open("GET","checkTeleServlet?tele=",true);
	 xhr.send(null);
	xhr.onreadystatechange = function() {
		if (xhr.readyState===xhr.DONE) {
			console.log(xhr.getAllResponseHeaders());
		}
	}
  </script> 
</head>
<body>
<form action="students.html" method="post"  enctype="multipart/form-data">
<p>
	<label>学号</label>
	<input type="number" name="stuno" value="" />
</p>
<p>
	<label>姓名</label>
	<input type="text" name="sname" value="" />
</p>
<p>
	<label>性别</label>
	<input type="text" name="sex" value="" />
</p>
<p>
	<label>电话</label>
	<input type="number" name="telephone" value="" id="telephone"/><!-- onblur="checkTele(this)" -->
</p>
<p>
	<label>qq</label>
	<input type="number" name="qq" value="" />
</p>
<p>
	<label>微信</label>
	<input type="text" name="wechat" value="" />
</p>
<p>
	<label>年级编号</label>
	<!-- <input type="number" name="gradeid" value="" /> -->
	<%-- <%
			Service2 s=new Service2();
			List<Grade> list=s.findALL();
			
			%> --%>
			<select name="gradeid" id="gradeid">
				<%-- <%
				 for(Grade g:list){
				%>
				<option value="<%=g.getId()%>"><%=g.getGname() %></option>
				<%} %> --%>
			</select>
</p>
<p>
	<label>入学日期</label>
	<input type="text" name="enterdate" value="" />
</p>
<p>
	<label>来自哪里</label>
	<input type="text" name="fromcity" value="" />
</p>
<p>
	<label>出生日期</label>
	<input type="text" name="birthday" value="" />
</p>
<p>
	<label>地址</label>
	<input type="text" name="address" value="" />
</p>
<p>
	<label>密码</label>
	<input type="number" name="pass" value="" />
</p>
<p>
	<label>图片</label>
	<input type="file" name="pic" value="" onchange='PreviewImage(this)'/>
</p>
<div id="imgPreview" style='width:500px; height:400px;'> 
    		<img src=""/> 
   		</div> 
<p>
	<input type="submit" value="添加">
</p>
</form>
</body>
</html>